﻿Imports System.Text
Imports Microsoft.VisualStudio.Utilities
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.Text.Editor
Imports VsSpeakerCompanion.UI
Imports System.Windows

Namespace VsSpeakerCompanion
    <Export(GetType(IWpfTextViewCreationListener)), ContentType("text"), TextViewRole(PredefinedTextViewRoles.Document)>
    Friend Class WidgetManagerFactory
        Implements IWpfTextViewCreationListener

        ' The ContentType attribute works for the specified content type or any content type inheriting from it.
        ' For example: the TextViewService will be executed when a "CSharp Signature Help" text view is created if we specify ContentType("CSharp") or ContentType("code")
        ' So we need to check if the content type is of the specific type we are looking for.
        Friend ReadOnly SupportedContentTypes() As String = {"CSharp", "Basic", "XML", "XAML", "CSS"}

        ''' <summary>
        ''' Export the Adornment Layer used by the VS Speaker
        ''' </summary>
        <Export(), Order(After:="Caret"), Name("VS Speaker Adornments Layer")>
        Private Property AdornmentLayer As AdornmentLayerDefinition

        Private Function GetOrderedWidgetProviders() As IEnumerable(Of IWidgetProvider)
            Return Orderer.Order(Me.WidgetProviders).Select(Function(export) export.Value)
        End Function

        ''' <summary>
        ''' Gets or sets the exported widget providers
        ''' </summary>
        <ImportMany()>
        Private Property WidgetProviders As IEnumerable(Of Lazy(Of IWidgetProvider, IOrderable))

        ''' <summary>
        ''' Exports an instance of <see cref="TwitterService"/>
        ''' </summary>
        <Export(GetType(TwitterAPI.TwitterService))>
        Private ReadOnly Property TwitterService As TwitterAPI.TwitterService
            Get
                Return WidgetManager.Instance.TwitterService
            End Get
        End Property

        ''' <summary>
        ''' Exports an instance of <see cref="IVisualStudioService"/>
        ''' </summary>
        <Export(GetType(IVisualStudioService))>
        Private ReadOnly Property VisualStudioService As IVisualStudioService
            Get
                Return WidgetManager.Instance
            End Get
        End Property

        ''' <summary>
        ''' Exports an instance of <see cref="IUIService"/>
        ''' </summary>
        <Export(GetType(IUIService))>
        Private ReadOnly Property UIService As IUIService
            Get
                Return WidgetManager.Instance.UIService
            End Get
        End Property

        <Import()>
        Private serviceProvider As IServiceProvider

        Public Sub TextViewCreated(ByVal textView As Microsoft.VisualStudio.Text.Editor.IWpfTextView) Implements Microsoft.VisualStudio.Text.Editor.IWpfTextViewCreationListener.TextViewCreated
            ' Check if the content type is supported
            If (textView.TextBuffer IsNot Nothing AndAlso SupportedContentTypes.Contains(textView.TextBuffer.ContentType.TypeName)) Then
                Dim manager = WidgetManager.Instance
                manager.ServiceProvider = serviceProvider
                If Not manager.IsInitialized Then
                    Try
                        manager.Initialize(Me.GetOrderedWidgetProviders())

                    Catch ex As Exception
                        ' TODO: Move string to resource file for easier localization
                        MessageBox.Show(String.Format("The Widget Manager could not be initialized. Error: '{0}'", ex.Message), "Error loading Demo Dashboard", MessageBoxButton.OK, MessageBoxImage.Error)
                    End Try
                End If

                ' If the widget manager could be initilized add the view
                If manager.IsInitialized Then
                    manager.Add(textView)
                End If
            End If
        End Sub


    End Class
End Namespace